//
//  ST_AnswersViewController.swift
//  btvideo
//
//  Created by TT mini on 2022/6/22.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_AnswersViewController: ST_BTLightBarViewController {

    var titleStr : String = ""
    var bodyStr : String = ""
    
    private lazy var titleView : UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.cornerRadius = 10
        
        return v
    }()
    
    private lazy var title_image_view : UIImageView = {
        let i = UIImageView()
        i.image = ST_ImageUtils.imageName("ques_detail_ask")
        
        return i
    }()
    
    private lazy var titleLabel : UILabel = {
        let l = UILabel()
        l.textColor = Color.init(hexString: "#333333")
        l.font = .systemFont(ofSize: 14, weight: .regular)
        l.textAlignment = .left
        
        return l
    }()
    
    private lazy var body_view : UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.cornerRadius = 10
        
        return v
    }()
    
    private lazy var body_image_view : UIImageView = {
        let i = UIImageView()
        i.image = ST_ImageUtils.imageName("ques_detail_answer")
        
        return i
    }()
    
    private lazy var body_label : UILabel = {
        let l = UILabel()
        l.textColor = Color.init(hexString: "#333333")
        l.font = .systemFont(ofSize: 14, weight: .regular)
        l.textAlignment = .left
        l.numberOfLines = 0
        l.lineBreakMode = .byWordWrapping
        
        return l
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title_nav_btn.setTitleForAllStates("问题详情")
        view.backgroundColor = Color.init(hexString: "#F5F8FA")
        own_navigation_bar.backgroundColor = Color.init(hexString: "#F5F8FA")
        make_u_i()
        deal_with_datas()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ST_AnswersViewController {
    
    func deal_with_datas() {
        
        //问题
        if titleStr.contains(".") {
            titleStr = titleStr.components(separatedBy: ".").last ?? ""
        }
        titleLabel.text = titleStr
        //答案
        let height = textHeightFromTextString(text: bodyStr, textWidth: ScreenWidth - 63 - 35, fontSize: 14, weight: .regular, lineSpacing: 0)
        body_view.snp.updateConstraints { make in
            make.height.equalTo(height + 19 + 25)
        }
        body_label.text = bodyStr
    }
    
    func make_u_i() {
        
        view.addSubviews([titleView,body_view])
        titleView.snp.makeConstraints { make in
            make.top.equalTo(own_navigation_bar.snp.bottom).offset(7)
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(ScreenWidth-30)
            make.height.equalTo(68)
        }
        
        titleView.addSubviews([title_image_view,titleLabel])
        title_image_view.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(19)
            make.height.equalTo(26)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(title_image_view.snp.right).offset(14)
            make.right.equalToSuperview().offset(-19)
            make.height.equalTo(20)
        }
        
        body_view.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(18)
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(ScreenWidth-30)
            make.height.equalTo(100)
        }
        
        body_view.addSubviews([body_image_view,body_label])
        body_image_view.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(18)
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(19)
            make.height.equalTo(26)
        }
        
        body_label.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(19)
            make.left.equalTo(body_image_view.snp.right).offset(14)
            make.right.equalToSuperview().offset(-19)
            make.bottom.equalToSuperview().offset(-25)
        }
    }
}

func textHeightFromTextString(text: String, textWidth: CGFloat, fontSize: CGFloat, weight: UIFont.Weight, lineSpacing: CGFloat) -> CGFloat {
    let style = NSMutableParagraphStyle()
    style.lineSpacing = lineSpacing
    
    let attributes = [
                    NSAttributedString.Key.font             : UIFont.systemFont(ofSize: fontSize, weight: weight),
                    NSAttributedString.Key.paragraphStyle   : style]
                
                as [NSAttributedString.Key : Any]
    
    let rect: CGRect = (text as NSString).boundingRect(with: CGSize(width: textWidth,height: CGFloat(MAXFLOAT)), options: [NSStringDrawingOptions.truncatesLastVisibleLine, NSStringDrawingOptions.usesFontLeading,NSStringDrawingOptions.usesLineFragmentOrigin],attributes: attributes ,context: nil)
    
    return rect.size.height+1
}
